/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

import beans.Rezervacija;

import java.sql.*;
import java.util.Calendar;
import java.util.Vector;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import javax.faces.component.UIComponent;
import javax.faces.validator.ValidatorException;
import java.util.ArrayList;
import java.util.Enumeration;
import javax.servlet.http.*;
/**
 *
 * @author Sasha Rakic
 */
public class Novi {
    private String username;
    private String password;
    private String checkpassword;
    private int kid;
    private String ime;
    private String prezime;
    private String email;
    private String funkcija ;
    private int isAdmin;

    public String register() {
        Connection con = null;
        ResultSet rs = null;
        Statement stmt = null;
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
        User u=(User)session.getAttribute("user");
        try {
            con = DB.getConnection();
            stmt = con.createStatement();
            rs = stmt.executeQuery("select * from korisnik where username='" + username + "';");
            if (rs.next()) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Изабрали сте већ постојеће корисничко име!Покушајте поново са други корисничким именом"));
                u.setOutcome("novik");
            } else if(!password.equals(checkpassword)){
                 FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Лозинка и потврда лозинке нису иста!"));
                 u.setOutcome("novik");
            }
              else{
                int admin;
                if(funkcija.equals("administrator")) admin=1;
                else admin=0;
                stmt.executeUpdate("insert into korisnik (username,lozinka,ime,prezime,email,funkcija,isAdmin) values ('" + username + "' ,'" + password + "' ,'" + ime + "' ,'" + prezime + "' ,'" + email + "' ,'" + funkcija + "' ," + admin + ");");
                username="";password="";ime="";prezime="";email="";funkcija="";
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Нови корисник је успешно унет!"));
                u.setOutcome("novik");
            }
            stmt.close();
            con.close();

        } catch (Exception e) {
            if (con != null) {
                try {
                    con.close();
                } catch (Exception ex) {
                }
            }
            String vrati="error";
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getLocalizedMessage()));
            return vrati;
        }
        String vrati="admin";
        return vrati;
    }

    public void validateEmail(FacesContext context,UIComponent componentToValidate, Object value) throws ValidatorException {
             String email1=(String)value;
   		     int i= email1.indexOf('@');
             int j=email1.indexOf('.',i+2);
             int k=email1.length();
             if(i>1 && j>0 && k>j+1) return; //ok e-mail adresa

            FacesMessage message=new FacesMessage("Неправилно унета ел. пошта!");
			message.setSeverity(FacesMessage.SEVERITY_ERROR);
        //  context.addMessage(componentToValidate.getClientId(context), message);
        //    prethodna linija koda je zakomentarisana, jer se radi automatski
        //    bacanjem odgovarajuceg Exception-a
			throw new ValidatorException(message);
    }

     public String getCheckpassword() {
        return checkpassword;
    }

    public void setCheckpassword(String checkpassword) {
        this.checkpassword = checkpassword;
    }


    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
     public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getKid() {
        return kid;
    }

    public void setKid(int kid) {
        this.kid = kid;
    }

    public String getIme() {
        return ime;
    }

    public void setIme(String ime) {
        this.ime = ime;
    }

    public String getPrezime() {
        return prezime;
    }

    public void setPrezime(String prezime) {
        this.prezime = prezime;
    }
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
    public String getFunkcija() {
        return funkcija;
    }

    public void setFunkcija(String funkcija) {
        this.funkcija = funkcija;
    }


}
